<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="__STATIC__/js/jquery.js"></script>
    <script src="__STATIC__/layui/layui.js"></script>
    <script type="text/javascript" src="__STATIC__/x-admin/js/xadmin.js"></script>
    <link rel="stylesheet" href="__STATIC__/x-admin/css/font.css">
    <link rel="stylesheet" href="__STATIC__/x-admin/css/xadmin.css">
    <link rel="stylesheet" href="__STATIC__/layui/css/layui.css">


    <link type="text/css" rel="stylesheet" href="__STATIC__/uploads/css/fileinput.css"/>
    <script type="text/javascript" src="__STATIC__/uploads/js/fileinput.js"></script>
    <script type="text/javascript" src="__STATIC__/uploads/js/fileinput_locale_zh.js"></script>
    <link type="text/css" rel="stylesheet" href="__STATIC__/uploads/css/bootstrap.min.css"/>

</head>
<body>
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="#">信息管理</a>
        <a href="#">消息分类</a>
        <a href="#">添加信息</a>
      </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:window.location.reload();" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>

<div style="width: 40%;padding-top: 10px">
    <form class="layui-form" name="addType" onsubmit="return false" id="type">
        <input type="hidden" id="new_video" name="video" >
        <div class="layui-form-item">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" placeholder="类别名称" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">类别</label>
            <div class="layui-input-block">
                <select name="type_id" id="type_id">
                    {foreach $type as $value}
                        <?php  $nbsp = str_repeat("&nbsp;",(substr_count($value['path'],","))*3); ?>
                        {if $value.parent_id === 0}
                            <option value="{$value.id}">{$nbsp}|--{$value.title}</option>
                        {else /}
                            <option value="{$value.id}">{$nbsp}|--{$value.title}</option>
                        {/if}
                    {/foreach}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">简介</label>
            <div class="layui-input-block">
                <textarea id="textarea" name="introduce" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">内容</label>
            <div class="layui-input-block">
                <textarea id="demo" style="display: none;"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">图片</label>
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="uploadImage">上传图片</button>
                <br/>
                <input type="hidden" name="images" value="">
                <img src="" width="100" height="100" style="display: none" id="imgs">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">视频</label>
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" id="video">上传视频</button>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" id="insert">立即提交</button>
                <button class="layui-btn layui-btn-primary" onclick="window.history.go(-1)">返回</button>
            </div>
        </div>
    </form>
</div>
</body>
</html>
<script>
    layui.use(['upload','layedit'], function() {
        var upload = layui.upload;
        layedit = layui.layedit;
        //上传图片
        upload.render({
            elem: "#uploadImage",
            url: "{:url('/common/img')}", //接口路径
            method: 'post', //可选项。HTTP类型，默认post
            data: {path: '/public/uploads/suggestion/img'}, //可选项。额外的参数，如：{id: 123, abc: 'xxx'}
            done: function (res) {
                if (res.img) {
                    //alert(res.img)
                    $("input[name=images]").val(res.img);
                    $("#imgs").attr('src', '__ROOT__\\uploads\\suggestion\\img\\' + res.img);
                    $("#imgs").css('display', 'block');
                    return
                } else {
                    layer.msg(res.code, {icon: 5, time: 2000});
                    return
                }
            },
            accept: 'images', //允许上传的文件类型
            size: 4096, //最大允许上传的文件大小
            auto: "true"
        });
        //视频
        upload.render({
            elem: "#video",
            url: "{:url('/common/video')}", //接口路径
            method: 'post', //可选项。HTTP类型，默认post
            accept: 'video', //允许上传的文件类
            auto: "true",
            before: function(obj){
                index = layer.msg('上传中请稍后', {
                    icon: 16
                    ,shade: 0.01
                    ,time:1000000
                });
            },
            done: function (res) {
                if (res.video) {
                    layer.msg('上传成功',{icon:1,time:2000});
                    $("#new_video").val(res.video);
                    return
                } else {
                    layer.msg(res,{icon:5,time:2000});
                    return
                }
            },

        });
        layedit = layui.layedit;
        layedit.set({
            uploadImage: {
                url: "{:url('/common/notice')}" //接口url
                ,type: 'post' //默认post
            }
        });
        index = layedit.build('demo',{
            tool: [
                'strong' //加粗
                ,'italic' //斜体
                ,'underline' //下划线
                ,'del' //删除线
                ,'left' //左对齐
                ,'center' //居中对齐
                ,'right' //右对齐
                ,'image' //插入图片
            ]}
        );
        //建立编辑器
        layedit.getContent(index)
    });
</script>
<script>
    $("#insert").click(function(){
        if(!$("input[name=title]").val()){
            layer.msg('标题不能为空',{icon:5,time:2000});
        }
        var content = layedit.getContent(index);
        var video = $("input[name=video]").val();
        var title = $("input[name=title]").val();
        var typeId = $("#type_id option:selected").val();
        var images = $("input[name=images]").val();
        var textarea = $("#textarea").val()
        $.ajax({
            url: "{:url('/suggestion/suggestionAdd')}",
            data: {
                content:content,
                video:video,
                title:title,
                typeId:typeId,
                images:images,
                textarea:textarea,
            },
            type: "post",
            dataType: "json",
            success: function(res){
                if(res.code){
                    layer.msg(res.msg,{icon:1,time:2000},function(){
                        window.location.reload();
                    });
                    return;
                }else{
                    layer.msg(res.msg,{icon:5,time:2000});
                    return;
                }
            }
        })
    })
</script>